package com.ddxcloud.movie.config.alipay;

import com.alipay.api.AlipayClient;
import com.alipay.api.AlipayConfig;
import com.alipay.api.DefaultAlipayClient;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Data
@Configuration
@ConfigurationProperties(prefix = "alipay")
public class AlipayConfiguration {

    /**
     * zh-H5
     */
    @Value("${alipay.zh_h5.app_id}")
    private String zh_h5_app_id;

    @Value("${alipay.zh_h5.merchant_private_key}")
    private String zh_h5_merchant_private_key;

    @Value("${alipay.zh_h5.alipay_public_key}")
    private String zh_h5_alipay_public_key;

    /**
     * dg-H5
     */
    @Value("${alipay.dg_h5.app_id}")
    private String dg_h5_app_id;

    @Value("${alipay.dg_h5.merchant_private_key}")
    private String dg_h5_merchant_private_key;

    @Value("${alipay.dg_h5.alipay_public_key}")
    private String dg_h5_alipay_public_key;

    /**
     * App
     */
    @Value("${alipay.app.app_id}")
    private String app_app_id;

    @Value("${alipay.app.merchant_private_key}")
    private String app_merchant_private_key;

    @Value("${alipay.app.alipay_public_key}")
    private String app_alipay_public_key;

    /**
     * Common
     */
    private String notify_url;

    private String return_url;

    private String sign_type;

    private String charset;

    private String format;

    private String gateway_url;

    /**
     * 默认为ZH-H5
     * @return
     */
    @Bean
    public AlipayClient setAlipayClient() {
        return new DefaultAlipayClient(
                gateway_url,
                zh_h5_app_id,
                zh_h5_merchant_private_key,
                format,
                charset,
                zh_h5_alipay_public_key,
                sign_type
        );
    }

    public AlipayConfig getAlipayConfig() {
        AlipayConfig alipayConfig = new AlipayConfig();
        alipayConfig.setServerUrl(gateway_url);
        alipayConfig.setAppId(app_app_id);
        alipayConfig.setPrivateKey(app_merchant_private_key);
        alipayConfig.setFormat(format);
        alipayConfig.setAlipayPublicKey(app_alipay_public_key);
        alipayConfig.setCharset(charset);
        alipayConfig.setSignType(sign_type);
        return alipayConfig;
    }
}
